import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/services.dart';
import 'package:flutter_jd/config/config.dart';
import 'package:flutter_jd/tools/utils/library.dart';
import 'package:flutter_jd/ui/widgets/kit.dart';

class Utils {
  // 判断当前是否处于degbu模式
  static bool isDebugMode() {
    return !bool.fromEnvironment("dart.vm.product");
  }

  // 颜色转换
  static Color hexToColor(String s) {
    return Color(int.parse(s.substring(1, 7), radix: 16) + 0xFF000000);
  }

  // Show Notification
  static showNotification() {
    BotToast.showSimpleNotification(
      title: "你有一条新动消息",
      backgroundColor: Colors.white,
      closeIcon: Kit.iconFont(0xe639, Color(0xFF333333), 22),
    );
  }

  // 获取token
  static String getToken() {
    return StorageUtil().getString('token') ?? '';
  }

  // 是否登录
  static bool isLogin() {
    return StorageUtil().getBool('isLogin') ?? false;
  }

  // 退出登录
  static logOut() {
    StorageUtil().remove('token');
    StorageUtil().setBool('isLogin', false);
  }

  // 隐藏键盘
  static void hideKeyboard(BuildContext context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus!.unfocus();
    }
  }

  // 显示状态栏
  static void showStatus() {
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
  }

  // 隐藏状态栏
  static void hideStatus() {
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
  }

  // 获取底部安全区域高度
  static getBottomHeight() {
    return MediaQueryData.fromWindow(window).padding.bottom;
  }

  // 获取当前时间距离上一次打开广告页面过去多少小时
  static int getStartTimeDifference() {
    final date = DateTime.now();

    // 上一次启动时间
    DateTime prevStartTime = DateTime.now();
    String prevTime = StorageUtil().getString('startTime') ?? '';

    if (prevTime.isNotEmpty) {
      try {
        prevStartTime = DateTime.parse(prevTime);
      } catch (e) {
        print(e);
        print('APP启动页启动时间解析错误');
      }
    } else {
      return Config.LAUNCH_INTERVAL;
    }
    return date.difference(prevStartTime).inHours;
  }
}
